package com.hnust.service.impl;

import com.hnust.dao.*;
import com.hnust.domain.*;
import com.hnust.domain.record.*;
import com.hnust.service.InfoManageService;
import com.hnust.util.IDUtil;
import com.hnust.util.JwtToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("infoManageService")
public class InfoManageImpl implements InfoManageService {

    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private TeaCourseGroupDao teaCourseGroupDao;
    @Autowired
    private CourseGroupDao courseGroupDao;
    @Autowired
    private CollegeDao collegeDao;
    @Autowired
    private AuditJoinDao auditJoinDao;
@Autowired
private GradRequirementsDao gradRequirementsDao;
    @Override
    public UserInfo getInfo(String id, String token) {
        List<TeaCourseGroup> teaCourseGroups = teaCourseGroupDao.getCourseGroupId(id);
        Map<String, String> map = null;
        UserInfo userInfo = new UserInfo();
        Teacher teacher = null;
        String idInToken;
        String passwordInToken;
        try {
            map = JwtToken.verifyToken(token);
            idInToken = map.get("id");
            passwordInToken = map.get("password");
        } catch (Exception e) {
            return null;
        }
        try {
            if (teacherDao.getTeacher(idInToken) == null) {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
        teacher = teacherDao.getTeacher(id);
        if(teacher==null){
            return null;
        }
        if (!teacher.getTeaId().equals(idInToken)) {
            return null;
        }

        if (!teacher.getPassword().equals(passwordInToken)) {
            return null;
        }
        try {
            userInfo.setName(teacher.getName());
            userInfo.setFaculty_number(teacher.getTeaNumber());
            userInfo.setEmail(teacher.getEMail());
            userInfo.setPhone(teacher.getPhoNumber());
            userInfo.setPassword(teacher.getPassword());
            userInfo.setCollegeId(teacher.getColId());
            userInfo.setSchool(teacherDao.getColName(teacher.getColId()));
            List<GroupInfo> groupInfoList = new ArrayList<>();
            for (TeaCourseGroup teaCourseGroup : teaCourseGroups) {
                GroupInfo groupInfo = new GroupInfo();
                groupInfo.setId(teaCourseGroup.getCouGroId());
                groupInfo.setName(courseGroupDao.getCourseGroupName(teaCourseGroup.getCouGroId()));
                groupInfo.setCourseId(courseGroupDao.getCourseIdByCourseGroupId(teaCourseGroup.getCouGroId()));
                groupInfoList.add(groupInfo);
            }
            userInfo.setGroup(groupInfoList);
            return userInfo;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public String updateCollege(String id, String token, String collegeId) {
        Map<String, String> map = null;
        try {
            map = JwtToken.verifyToken(token);
        } catch (Exception e) {
            return "无效令牌";
        }
        String idInToken = map.get("id");
        String passwordInToken = map.get("password");

        Teacher teacher = null;
        if (teacherDao.getTeacher(idInToken) == null) {
            return "用户不存在";
        }
        teacher = teacherDao.getTeacher(id);
        if(teacher==null){
            return "用户不存在";
        }
        if (!idInToken.equals(teacher.getTeaId())) {
            return "用户不匹配";
        }
        if (!teacher.getPassword().equals(passwordInToken)) {
            return "用户不匹配";
        }

        if (collegeDao.getCollege(collegeId) != null) {
            try {
                teacherDao.updateCollege(collegeId, id);
            } catch (Exception e) {
                return "修改学院失败";
            }
            return "修改学院成功";
        } else {
            return "学院不存在";
        }

    }

    @Override
    @Transactional
    public String updateInfo(UpdateUserInfo updateUserInfo, String token, String id) {
        Map<String, String> map = null;
        try {
            map = JwtToken.verifyToken(token);
        } catch (Exception e) {
            return "无效令牌";
        }
        String idInToken = map.get("id");
        String passwordInToken = map.get("password");
        if(teacherDao.getTeacher(idInToken).getTeaId()==null){
            return "用户不存在";
        }
        Teacher teacher=null;
        teacher = teacherDao.getTeacher(id);
        try{
            if(!idInToken.equals (teacher.getTeaId())){
                return "用户不匹配";
            }
            if(!passwordInToken.equals(teacher.getPassword())){
                return "用户不匹配";
            }
        }catch (Exception e){
            return "用户不存在";
        }
        try{
            teacherDao.updateInfo(updateUserInfo.getName(),updateUserInfo.getPhone(),
                    updateUserInfo.getEmail(),id,updateUserInfo.getNumber(),updateUserInfo.getCollegeId()
            ,updateUserInfo.getPassword());
        }catch (Exception e){
            return "修改信息失败";
        }
        return  "修改信息成功";
    }
    @Override
    public List<GroupInfo> getCourseGroup(String collegeId, String token){

        Map<String, String> map = null;
        try {
            map = JwtToken.verifyToken(token);
        } catch (Exception e) {
            return null;
        }
        String idInToken = map.get("id");
        if(teacherDao.getTeacherIdById(idInToken)==null){
            return null;
        }
           List<CourseGroup> courseGroups= courseGroupDao.getCourseGroup(collegeId);
           if(courseGroups.size()==0){
               return null;
           }
           List<GroupInfo> GroupInfoList = new ArrayList<>();
           for(CourseGroup courseGroup : courseGroups){
               GroupInfo groupInfo = new GroupInfo();
               groupInfo.setId(courseGroup.getCouGroId());
               groupInfo.setName(courseGroup.getName());
               GroupInfoList.add(groupInfo);
           }

        return GroupInfoList;
    }

    @Override
    public String applyCourse(String id,String groupId,String token){
        Map<String, String> map = null;
        id =id.replace("\"","");
        groupId=groupId.replace("\"","");
        token = token.replace("\"","");
        try {
            map = JwtToken.verifyToken(token);


        } catch (Exception e) {
            return "无效令牌";
        }

        String idInToken = map.get("id");
        String passwordInToken = map.get("password");

        Teacher teacher = null;
        if (teacherDao.getTeacher(idInToken) == null) {
            return "用户不存在";
        }
        teacher = teacherDao.getTeacher(id);
        if(teacher==null){
            return "用户不存在";
        }
        if (!idInToken.equals(teacher.getTeaId())) {
            return "用户不匹配";
        }
        if (!teacher.getPassword().equals(passwordInToken)) {
            return "用户不匹配";
        }
        String auditJoinId = IDUtil.getPrimaryKey();
        try{
            auditJoinDao.addAuditJoin(auditJoinId,1,id,groupId);
        }catch (Exception e){
            return "申请加入课程组失败";
        }
        return "申请加入课程组成功";
    }

    @Override
    public String outCourse(String id,String groupId,String token){
        Map<String, String> map = null;
        id =id.replace("\"","");
        groupId=groupId.replace("\"","");
        token = token.replace("\"","");
        try {
            map = JwtToken.verifyToken(token);
        } catch (Exception e) {
            return "无效令牌";
        }
        String idInToken = map.get("id");
        String passwordInToken = map.get("password");

        Teacher teacher = null;
        if (teacherDao.getTeacher(idInToken) == null) {
            return "用户不存在";
        }
        teacher = teacherDao.getTeacher(id);
        if(teacher==null){
            return "用户不存在";
        }
        if (!idInToken.equals(teacher.getTeaId())) {
            return "用户不匹配";
        }
        if (!teacher.getPassword().equals(passwordInToken)) {
            return "用户不匹配";
        }
        try{
            teaCourseGroupDao.deleteTeaCourseGroup(groupId,id);
            teaCourseGroupDao.deleteAuditJoin(groupId,id);
        }catch (Exception e){
            return "退出课程组失败";
        }
        return "退出课程组成功";
    }

    @Override
    public GroupResult getCourseInfo(String id,String groupId,String token, Integer nowPage){
        Map<String, String> map = null;
        try {
            map = JwtToken.verifyToken(token);
        } catch (Exception e) {

            return null;
        }
        String idInToken = map.get("id");
        String passwordInToken = map.get("password");

        Teacher teacher = null;
        if (teacherDao.getTeacher(idInToken) == null) {

            return null;
        }
        teacher = teacherDao.getTeacher(id);
        if(teacher==null){

            return null;
        }
        if (!idInToken.equals(teacher.getTeaId())) {

            return null;
        }
        if (!teacher.getPassword().equals(passwordInToken)) {

            return null;
        }
        GroupResult groupResult=new GroupResult();
       Teacher leader= teacherDao.getTeacher(teaCourseGroupDao.getLeaderByGroupId(groupId,1));//获取组长信息
        GroupData groupData=new GroupData();
        groupData.setLeaderNumber(leader.getTeaNumber());
        groupData.setLeaderName(leader.getName());
        List<Teacher> teachers=teaCourseGroupDao.getTeacherByGroupId(groupId,10*nowPage,10);
        List<GroupMember> groupMemberList = new ArrayList<>();
        for(Teacher teacherId : teachers){
            GroupMember groupMember = new GroupMember();
            groupMember.setId(teacherDao.getTeacher(teacherId.getTeaId()).getTeaId());
            groupMember.setFacultyNumber(teacherDao.getTeacher(teacherId.getTeaId()).getTeaNumber());
            groupMember.setName(teacherDao.getTeacher(teacherId.getTeaId()).getName());
            groupMember.setPhone(teacherDao.getTeacher(teacherId.getTeaId()).getPhoNumber());
            groupMemberList.add(groupMember);

        }
        groupData.setGroupMember(groupMemberList);
        groupResult.setTotal(teaCourseGroupDao.findTeacherCountByGroupId(groupId));
        groupResult.setGroupData(groupData);
        return groupResult;
    }
    @Override
    public List<GroupMember> getCourseTeaByName(String teacherName,String token,String groupId){

        List<Teacher> teacher=teaCourseGroupDao.getTeacherByGroId(groupId);
       List<Teacher>  teacherIdByName = teacherDao.getTeacherByName(teacherName);//通过姓名查到的老师信息

        List<GroupMember> groupMemberList = new ArrayList<>();
        try{
            for(Teacher teacherIds : teacher){
                for(Teacher teacherId : teacherIdByName){
                    if(teacherId.getTeaId().equals(teacherIds.getTeaId())){
                        GroupMember groupMember = new GroupMember();
                        groupMember.setId(teacherId.getTeaId());
                        groupMember.setFacultyNumber(teacherId.getTeaNumber());
                        groupMember.setName(teacherId.getName());
                        groupMember.setPhone(teacherId.getPhoNumber());
                        groupMemberList.add(groupMember);
                }

                }
            }
        }catch (Exception e){
            return null;
        }
        return groupMemberList;
    }

    @Override
    public List<CoursePoint> getCoursePoint(String majorId,String token,String id){
        Map<String, String> map = null;
        try {
            map = JwtToken.verifyToken(token);
        } catch (Exception e) {
            return null;
        }
        String idInToken = map.get("id");
        String passwordInToken = map.get("password");

        Teacher teacher = null;
        if (teacherDao.getTeacher(idInToken) == null) {
            return null;
        }
        teacher = teacherDao.getTeacher(id);
        if(teacher==null){
            return null;
        }
        if (!idInToken.equals(teacher.getTeaId())) {
            return null;
        }
        if (!teacher.getPassword().equals(passwordInToken)) {
            return null;
        }
        List<GradRequirements> gradRequirements=gradRequirementsDao.getGradReqByMajorId(majorId);

        CoursePoint coursePoint = new CoursePoint();
        List<CoursePoint> coursePointList = new ArrayList<>();
        for(GradRequirements gradRequirement : gradRequirements){
            coursePoint.setGradId(gradRequirement.getGradId());
            coursePoint.setContent(gradRequirement.getContent());
            coursePointList.add(coursePoint);
        }
        return coursePointList;
    }
    public String updateCoursePoint(List<CoursePoint> coursePoint,String token,String id){
        Map<String, String> map = null;
        try {
            map = JwtToken.verifyToken(token);
        } catch (Exception e) {
            return "无效令牌";
        }
        String idInToken = map.get("id");
        String passwordInToken = map.get("password");
        Teacher teacher = null;
        if (teacherDao.getTeacher(idInToken) == null) {
            return "用户不存在";
        }
        teacher = teacherDao.getTeacher(id);
        if(teacher==null){

            return "用户不存在";

        }
        if (!idInToken.equals(teacher.getTeaId())) {
            return "用户不匹配";
        }
        if (!teacher.getPassword().equals(passwordInToken)) {
            return "用户不匹配";
        }
        try{
            for(CoursePoint couPoint:coursePoint) {

                gradRequirementsDao.updateContent(couPoint.getContent(), couPoint.getGradId());
            }
        }catch (Exception e){
            return "修改毕业要求指标点失败";
        }
        return "修改毕业要求指标点成功";
    }

    @Override
    public List<GroupInfo> getWaitCourseGroup(String teacherId, String token) {
        Map<String, String> map = null;
        try {
            map = JwtToken.verifyToken(token);
        } catch (Exception e) {
            return null;
        }
        String idInToken = map.get("id");
        if(teacherDao.getTeacherIdById(idInToken)==null){
            return null;
        }

        List<GroupInfo> GroupInfoList= courseGroupDao.getWaitCourseGroup(teacherId);
        if(GroupInfoList.size()==0){
            return null;
        }
        return GroupInfoList;
    }
}





